<template>
  <el-dialog
    title="请选择用户"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="dialogShow"
    :before-close="close"
    class="product_dialog dialog900 small-padding-dialog"
  >
    <div class="mainor_form batch-wrap">
      <el-form
        :inline="true"
        :model="productForm"
        class="demo-form-inline"
        ref="addUserFrom"
        label-width="80px"
      >
        <el-form-item label="用户分组：" prop="group_ids" class="wid180">
          <el-select
            v-model="productForm.group_ids"
            placeholder="请选择"
            class="wid180"
            filterable
            multiple
            collapse-tags
            clearable
            @change="handleIdsSelect"
          >
            <el-option
              v-for="statusList in userGroundList"
              :key="statusList.id"
              :label="statusList.title"
              :value="statusList.id"
              :disabled="statusList.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户" prop="name">
          <el-input
            v-model.trim="productForm.name"
            placeholder="请输入用户名称"
            clearable
            class="wid180"
            maxlength="10" show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button class="reset mg-r-6" type="primary" @click="reset">重置</el-button>
        <el-button type="primary" :loading="listLoading" @click="search">搜索</el-button>
      </div>
    </div>
    <div class="main_table mg-t-16">
      <el-table
        ref="productTable"
        v-loading="listLoading"
        :data="tableData"
        element-loading-text="加载中"
        fit
        border
        highlight-current-row
        height="400"
        @select="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column label="用户名称" prop="nickname" align="cneter" />
      </el-table>
    </div>
    <div class="main_page boxdq">
      <!-- 分页 -->
      <div></div>
      <div class="main_page_r">
        <MainPagination
          :current-page="currentPage"
          :page-sizes="[10, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="changeLimit"
          @current-change="changePage"
        />
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getInfo } from '@/api/menu.js'
export default {
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    disabledList: {
      type: Array,
      default: () => []
    },
    options: {
      type: Array,
      default: () => []
    },
    selectGroup: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // 筛选商品
      shield: false,
      productForm: {
        category_id: [],
        name: ''
      },
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
      listLoading: false,
      currentPage: 1,
      total: 1,
      pageSize: 10,
      tableData: [],
      selectList: [],
      userGroundList: []
    }
  },
  watch: {
    disabledList: {
      handler(val) {
        this.selectList = JSON.parse(JSON.stringify(val));
      },
      deep: true,
      immediate:true,
    }
  },
  methods: {
    changeCheckbox(row) {
      this.$refs.productTable.toggleRowSelection(row, !row.is_selected)
      
    },
    selectable(row) {
      // return !this.disabledList.map(el => el.id).includes(row.id)
    },
    close() {
      this.currentPage = 1
      this.tableData = [];
      this.productForm.group_ids = []
      this.productForm.name = ''
      this.$emit('close')
    },
    getData(group_ids) {
      // if(group_ids.length) this.productForm.group_ids = group_ids
      this.getTableData()
      getInfo('/common/getUserGroup', {}).then(res => {
        if(res.data) {
          this.userGroundList = res.data.map(el => {
            el.disabled = false
            el.id = el.id.toString()
            return el
          })  
        }
      })
    },
    getTableData() {
      this.listLoading = true
      getInfo('/packageMenu/memberList', {
        page: this.currentPage,
        pagesize: this.pageSize,
        group_id: this.productForm.group_ids.join(','),
        name: this.productForm.name,
        select_group: this.selectGroup.length ? this.selectGroup.join(',') : '-2'
      }).then(res => {
        if(res && res.data) {
          this.tableData = res.data.list.map(el => {
            el.is_selected = this.disabledList.includes(el.is_selected)
            return el
          })
          // this.$nextTick(() => {
          //   this.tableData.forEach(el => {
          //     if(el.selected) this.$refs.productTable.toggleRowSelection(el, true);
          //   })
          // })
          if(this.disabledList.length) {
            setTimeout(() => {
              this.tableData.forEach(row => {
                if(this.disabledList.find(item=>item.id==row.id)) {
                  this.$refs.productTable.toggleRowSelection(row, true);
                }
              })
            }, 500)
          }
          this.total = res.data.total
          this.listLoading = false
        }
      })
    },
    reset() {
      this.$refs.addUserFrom.resetFields()
      this.search()
    },
    search() {
      this.currentPage = 1
      this.getTableData()
    },
    changeLimit(page) {
      this.pageSize = page
      this.getTableData()
    },
    changePage(page) {
      this.currentPage = page
      this.getTableData()
    },
    handleSelectionChange(val, row) {
      const list = val.map(el => el.id)
      this.tableData.forEach(el => {
        el.is_selected = list.includes(el.id)
      })
      let index = this.selectList.findIndex(item=> item.id == row.id);
      if(index !== -1) {
        this.selectList.splice(index,1);
      }else {
        this.selectList.push(row);
      }
    },
    submit() {
      this.$emit('submit', this.selectList)
      this.close()
    },
    handleIdsSelect(val) {
      if(!val.length) {
        this.userGroundList.forEach(el => el.disabled = false)
      }else {
        if(val.includes('-1')) {
          this.userGroundList.forEach(el => el.disabled = !val.includes(el.id))
        }else {
          this.userGroundList.forEach(el => {
            if(el.id === '-1') el.disabled = true
          })
        }
      }
    }
  },
}
</script>
<style scoped lang="scss">
.product_dialog {
  height: 835px;
  /deep/.el-dialog__header {
    border-bottom: 1px solid #EBEEF5;
  }
  /deep/.el-dialog__body {
    padding: 0 20px;
  }
  .mainor_form {
    margin-top: 12px;
  }
  /deep/.el-button--primary {
    background-color: #1BA88E;
    color: #fff;
    &:hover {
      background-color: rgba(27,168,142, .7);
      border-color: rgba(27,168,142, .7);
    }
    &.reset {
      background-color: #fff;
      border: 1px solid #03A98E;
      color: #03A98E;
      &:hover {
        background-color: rgba(27,168,142, .3);
        border-color: rgba(27,168,142, .3);
      }
    }
  }
  .is-selectd {
    cursor: pointer;
    color: rgba(0, 0, 0, .75);
    &.add{
      color: #42BEAA;
    }
    &.delete{
      color: #FF7785;
    }
  }
  /deep/.el-dialog__footer {
    border-top: 1px solid #EBEEF5;
    margin-top: 12px;
  }
  .mg-t-16 {
    margin-top: 16px;
  }
}
</style>